﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLPKWeb.Models.FormBean.DanhMucChungModel;
using QLPKWeb.Models.LinQ.DanhMucChung;
using Webdiyer.WebControls.Mvc;
using DONGACS.Common;

namespace QLPKWeb.BussinessDelegate.DanhMucChung
{
    public class TaiKhoanNganHangDelegate
    {
        DBDanhMucChungDataContext DBDanhMucChung = new DBDanhMucChungDataContext();

        public IList<dw_TaiKhoanNganHang> GetAlldw_TaiKhoanNganHang()
        {
            return DBDanhMucChung.dw_TaiKhoanNganHangs.ToList();
        }
        public dw_TaiKhoanNganHang AddNewTaiKhoanNganHang(dw_TaiKhoanNganHang dw_TaiKhoanNganHang)
        {
            DBDanhMucChung.dw_TaiKhoanNganHangs.InsertOnSubmit(dw_TaiKhoanNganHang);
            DBDanhMucChung.SubmitChanges();
            return GetTaiKhoanNganHangIdMax();
        }

        public void DeleteTaiKhoanNganHang(string dw_TaiKhoanNganHangID)
        {
            dw_TaiKhoanNganHang dw_TaiKhoanNganHangNew =
                DBDanhMucChung.dw_TaiKhoanNganHangs.Single(p => p.Id == Convert.ToInt32(dw_TaiKhoanNganHangID));
            //dw_TaiKhoanNganHang.Id = dw_TaiKhoanNganHang.Id;

            DBDanhMucChung.dw_TaiKhoanNganHangs.DeleteOnSubmit(dw_TaiKhoanNganHangNew);
            DBDanhMucChung.SubmitChanges();
        }

        public void UpdateTaiKhoanNganHang(dw_TaiKhoanNganHang dw_TaiKhoanNganHang)
        {
            dw_TaiKhoanNganHang dw_TaiKhoanNganHangUpdate =
            DBDanhMucChung.dw_TaiKhoanNganHangs.Single(p => p.Id == dw_TaiKhoanNganHang.Id);
            dw_TaiKhoanNganHangUpdate.Id = dw_TaiKhoanNganHang.Id;
            dw_TaiKhoanNganHangUpdate.DepositCode = dw_TaiKhoanNganHang.DepositCode;
            dw_TaiKhoanNganHangUpdate.DepositKind = dw_TaiKhoanNganHang.DepositKind;
            dw_TaiKhoanNganHangUpdate.BankId = dw_TaiKhoanNganHang.BankId;
            dw_TaiKhoanNganHangUpdate.IdNgoaiTe = dw_TaiKhoanNganHang.IdNgoaiTe;
            dw_TaiKhoanNganHangUpdate.SwifCode = dw_TaiKhoanNganHang.SwifCode;
            dw_TaiKhoanNganHangUpdate.BankAccountNo = dw_TaiKhoanNganHang.BankAccountNo;
            dw_TaiKhoanNganHangUpdate.Custommer = dw_TaiKhoanNganHang.Custommer;
            DBDanhMucChung.SubmitChanges();
        }
        public PagedList<dw_TaiKhoanNganHang> SearchTaiKhoanNganHangPagedList(int? id, string searchCriteria)// id = page Index
        {
            return (PagedList<dw_TaiKhoanNganHang>)DBDanhMucChung.dw_TaiKhoanNganHangs.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage).Where(p => p.Custommer == searchCriteria);
        }
        public dw_TaiKhoanNganHang GetTaiKhoanNganHangById(string id)
        {
            try
            {
                dw_TaiKhoanNganHang item = this.DBDanhMucChung.dw_TaiKhoanNganHangs.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }
        public dw_TaiKhoanNganHang GetTaiKhoanNganHangByTen(string Ten)
        {
            IEnumerable<dw_TaiKhoanNganHang> list = this.DBDanhMucChung.dw_TaiKhoanNganHangs.Where(p => p.Custommer == Ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public PagedList<dw_TaiKhoanNganHang> GetTaiKhoanNganHangPagedList(int? id)// id = page Index
        {
            return DBDanhMucChung.dw_TaiKhoanNganHangs.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }
        public SelectList BuilTaiKhoanNganHang(int? selectedValue)
        {
            IList<dw_TaiKhoanNganHang> selectNCC = DBDanhMucChung.dw_TaiKhoanNganHangs.OrderBy(p => p.Id).ToList();
            IList<object> selectList = new List<object>();
            int dem = 0;
            foreach (var o in selectNCC)
            {
                dem++;
                if (selectedValue != 0 || dem == selectedValue)
                {
                    selectList.Add(new { value = o.Id, Text = o.Custommer, SelectedValue = selectedValue });
                }
                else
                {
                    selectList.Add(new { value = o.Id, Text = o.Custommer });
                }
            }
            SelectList select = new SelectList(selectList, "Value", "Text", selectedValue);
            return select;
        }
      
        public dw_TaiKhoanNganHang GetTaiKhoanNganHangIdMax()
        {
            IList<dw_TaiKhoanNganHang> list = DBDanhMucChung.dw_TaiKhoanNganHangs.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public dw_TaiKhoanNganHang BuildNewTaiKhoanNganHang(TaiKhoanNganHangForm form)
        {
            dw_TaiKhoanNganHang newdw_TaiKhoanNganHang = new dw_TaiKhoanNganHang();
            newdw_TaiKhoanNganHang.Custommer = form.Custommer;
            newdw_TaiKhoanNganHang.DepositCode = form.DepositCode;
            newdw_TaiKhoanNganHang.DepositKind = form.DepositKind;
            newdw_TaiKhoanNganHang.BankAccountNo = form.BankAccountNo;
            newdw_TaiKhoanNganHang.BankId = form.BankId;
            newdw_TaiKhoanNganHang.SwifCode = form.SwifCode;
            newdw_TaiKhoanNganHang.IdNgoaiTe = form.IdNgoaiTe;
            return newdw_TaiKhoanNganHang;
        }

        public dw_TaiKhoanNganHang BuildEditTaiKhoanNganHang(TaiKhoanNganHangForm form)
        {
            dw_TaiKhoanNganHang newdw_TaiKhoanNganHang = new dw_TaiKhoanNganHang();
            newdw_TaiKhoanNganHang.Id = form.Id;
            newdw_TaiKhoanNganHang.Custommer = form.Custommer;
            newdw_TaiKhoanNganHang.DepositCode = form.DepositCode;
            newdw_TaiKhoanNganHang.DepositKind = form.DepositKind;
            newdw_TaiKhoanNganHang.BankAccountNo = form.BankAccountNo;
            newdw_TaiKhoanNganHang.BankId = form.BankId;
            newdw_TaiKhoanNganHang.SwifCode = form.SwifCode;
            newdw_TaiKhoanNganHang.IdNgoaiTe = form.IdNgoaiTe;
            return newdw_TaiKhoanNganHang;
        }

    

    }
}